
'use strict';
const BaseService = require('./base');
const _ = require('lodash');

class CategoryService extends BaseService {

  async findByRecommend() {
    return await this.ctx.model.Category.findAll({
      where: {
        recommend: true,
        online: true
      },
      order: [
        ['index', 'ASC']
      ]
    })
  }

  async add(params) {
    return this.run(async () => {
      const { ctx } = this;
      const result = await ctx.model.Category.create(params);
      return result;
    });
  }
  async edit(id, params) {
    return this.run(async () => {
      const { ctx } = this;
      const result = await ctx.model.Category.findOne({
        where: {
          id,
        },
      });
      !result && ctx.helper.commonErr('未查询到数据', {});
      await ctx.model.Category.update(params, {
        where: {
          id,
        },
      });
    });
  }
  async query(id) {
    return this.run(async () => {
      const { ctx, app } = this;
      const Op = app.Sequelize.Op;
      let result = null;
      if (id) {
        result = await ctx.model.Category.findOne({
          where: {
            id,
            delete_time: {
              [Op.is]: null,
            },
          },
        });
      } else {
        result = await ctx.model.Category.findAll({
          where: {
            delete_time: {
              [Op.is]: null,
            },
          },
          order: [
            [ 'create_time', 'desc' ],
          ],
        });
      }
      return result;
    });
  }

  // 查询用户发过的话题
  async queryUserPublish(id) {
    const { ctx, app } = this;
    const Op = app.Sequelize.Op;
    const thems = await ctx.model.Theme.findAll({
      where: {
        user_id: id,
        online: true,
      },
      attributes: [[ctx.model.fn('DISTINCT', ctx.model.col('category_id')), 'categoryId']],
    });
    let themsCategorys = JSON.parse(JSON.stringify(thems));
    themsCategorys = _.map(themsCategorys, 'categoryId');
    
    let publishCategorys = await ctx.model.Category.findAll({
      where: {
        id: {
          [Op.or]: themsCategorys,
        },
        delete_time: {
          [Op.is]: null,
        },
      },
    });
    for (let i = 0; i < publishCategorys.length; i ++) {
      const totals = await ctx.model.Theme.findAndCountAll({
        where: {
          user_id: id,
          online: true,
          category_id: publishCategorys[i].dataValues.id,
        }
      });
      publishCategorys[i].dataValues.totals = totals.count;
    }
    const totals = await ctx.model.Theme.findAndCountAll({
      where: {
        user_id: id,
        online: true,
      }
    });
    publishCategorys = [
      {id: '', name: '全部', title: '全部', totals: totals.count},
      ...publishCategorys
    ]
    return publishCategorys;
  }

  async delete(id, delete_time) {
    return this.run(async () => {
      const { ctx } = this;
      const deleteValue = await ctx.model.Category.findOne({
        where: {
          id,
        },
        attributes: [ 'delete_time' ],
      });
      !deleteValue && ctx.helper.commonErr('未查询到数据', {});

      // 判断数据是否已删除
      (deleteValue.dataValues.delete_time) && ctx.helper.commonErr('请勿重复删除', {});
      const result = await ctx.model.Category.update({ delete_time }, {
        where: {
          id,
        },
      });
      return result;
    });
  }
}

module.exports = CategoryService;
